/**
 * Copyright 2010 Jake Jackson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jake.nb.ext.ant.gotoimpl;

import java.io.File;
import java.util.EnumSet;
import java.util.Set;
import javax.swing.text.Document;
import org.apache.tools.ant.module.api.AntProjectCookie;
import org.jake.nb.ext.ant.parser.AntParser;
import org.netbeans.lib.editor.hyperlink.spi.HyperlinkProviderExt;
import org.netbeans.lib.editor.hyperlink.spi.HyperlinkType;
import org.openide.util.NbBundle;

public class GotoDefinitionHyperlink implements HyperlinkProviderExt {

  public Set<HyperlinkType> getSupportedHyperlinkTypes() {
    return EnumSet.of(HyperlinkType.GO_TO_DECLARATION, HyperlinkType.ALT_HYPERLINK);
  }

  public boolean isHyperlinkPoint(final Document doc, final int offset, final HyperlinkType type) {
    return getHyperlinkSpan(doc, offset, type) != null;
  }

  public int[] getHyperlinkSpan(final Document doc, final int offset, final HyperlinkType type) {
    return GotoDefinitionHelper.getSpan(doc, offset, type);
  }

  public void performClickAction(final Document doc, final int offset, final HyperlinkType type) {
    GotoDefinitionHelper.gotoDefinition(doc, offset, type);
  }

  public String getTooltipText(final Document doc, final int offset, final HyperlinkType type) {
    String retValue = null;
    final String targetName = GotoDefinitionHelper.getTargetName(doc, offset, type);

    if (!"".equals(targetName)) {
      final File fileForTargetName = AntParser.getFileForTargetName(doc, targetName, offset);
      if (fileForTargetName != null) {
        // TODO: replace by bundle
        String definedIn = " " + NbBundle.getMessage(this.getClass(), "LBL_DefinedIn") + " ";  // NOI18N
        final AntProjectCookie projectCookie = AntParser.getProjectCookie(doc);

        if (projectCookie != null && fileForTargetName.equals(projectCookie.getFile())) {
          definedIn += NbBundle.getMessage(this.getClass(), "LBL_ThisFile");  // NOI18N
        } else {
          definedIn += fileForTargetName;
        }

        retValue = "\"" + targetName + "\"" + definedIn; // NOI18N
      }
    }

    return retValue;
  }
}
